package com.bianguo.android.tencentpushdemo.view.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bianguo.android.tencentpushdemo.adapters.TopPagerAdapter;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/4.
 */

public class TopStoryPager extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private Context mContext;
    private ViewPager mViewPager;
    private Timer mTimer;
    private int currentItem;
    private int totalItems;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (0 == currentItem) {
                        mViewPager.setCurrentItem(currentItem, false);
                    } else {
                        mViewPager.setCurrentItem(currentItem);
                    }
                    break;
            }
        }
    };

    public TopStoryPager(Context context) {
        super(context);
        initView(context);
    }

    public TopStoryPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TopStoryPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context mContext) {
        this.mContext = mContext;
        mViewPager = new ViewPager(mContext);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mViewPager.setLayoutParams(layoutParams);

        addView(mViewPager);
    }

    public void init(ArrayList<String> items){
        if (items == null || items.size() <= 0) {
            return;
        }
        ArrayList<ImageView> imageViews = new ArrayList<>();
        totalItems = items.size();
        for (int i = 0; i < totalItems; i++) {
            ImageView imageView = new ImageView(mContext);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(layoutParams);
            imageViews.add(imageView);
            Glide.with(mContext)
                    .load(items.get(i))
                    .centerCrop()
                    .into(imageView);
        }
        TopPagerAdapter topPagerAdapter = new TopPagerAdapter(imageViews);
        mViewPager.setAdapter(topPagerAdapter);
        mViewPager.addOnPageChangeListener(this);
    }

    public void startTimer(long time){
        if (mTimer == null) {
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    currentItem = (currentItem + 1) % totalItems;
                    mHandler.sendEmptyMessage(0);
                }
            }, time, time);
        }
    }

    public void stopTimer(){
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        currentItem = i;
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
